﻿/*===================================================================================
	TASK 6:                                                                  {Arrays}
	Write a program that reads two integer numbers N and K and an array of N elements
    from the console. Find in the array those K elements that have maximal sum.
===================================================================================*/

using System;

class MaximalSumFromKElement
{
    static void Main()
    {
        Console.Write("Enter N=");
        int n = int.Parse(Console.ReadLine());

        Console.Write("Enter K=");
        int k = int.Parse(Console.ReadLine());

        int[] arr = new int[n];

        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write("arr[{0}]=", i);
            arr[i] = int.Parse(Console.ReadLine());
        }

        //int n = 9;
        //int k = 4;
        //int[] arr = { 3, 0, 5, 4, 10, 1, 45, 5, 30 };

        int sum = 0;
        int maxSum = 0;
        int numberOfMembers = k;

        if (k < n)
        {
            for (int i = 0; i <= n - numberOfMembers; i++, k++)
            {
                for (int j = i; j < k; j++)
                {
                    sum = sum + arr[j];
                }
                if (sum > maxSum)
                {
                    maxSum = sum;
                }
                sum = 0;
            }
            Console.WriteLine(maxSum);
        }
        else
        {
            Console.WriteLine("Enter correct K(K<N)");
        }
    }
}

